package com.dci.service.impl.schedule;

import com.dci.dao.pms.schedule.ProjectNodeInfoMapper;
import com.dci.dao.pms.schedule.ScheduleTrackInfoMapper;
import com.dci.model.schedule.ProjectNodeInfo;
import com.dci.model.schedule.ScheduleTrackInfo;
import com.dci.result.JsonData;
import com.dci.result.PageResult;
import com.dci.result.schedule.ScheduleResult;
import com.dci.service.schedule.ScheduleTrackInfoService;
import com.dci.util.UUIDBuild;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;


/**
 * All rights Reserved, Designed By 姑苏区政府大数据中心
 *
 * @version V1.0
 * @Title: null.java
 * @Package com.dci.service.impl.schedule
 * @Description: 进度跟踪实现类
 * @author: 张星亮
 * @date: 2019/2/25
 * @Copyright: 2019 姑苏区政府大数据中心. All rights reserved.
 * 注意：本内容仅限于姑苏区政府大数据中心内部传阅，禁止外泄以及用于其他的商业目
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class ScheduleTrackInfoServiceImpl implements ScheduleTrackInfoService {

    @Resource
    private ScheduleTrackInfoMapper scheduleTrackInfoMapper;

    @Resource
    private ProjectNodeInfoMapper projectNodeInfoMapper;

    /**
     * 新增
     *
     * @param record
     * @return
     */
    @Override
    public JsonData insert(ScheduleTrackInfo record) {
        record.setId(UUIDBuild.getUUID());
        List<ProjectNodeInfo> list1 = record.getNodeList();
        String name = list1.get(0).getNodeName();
        record.setNode_name(name);
        scheduleTrackInfoMapper.insert(record);

        // 批量添加节点列表
        List<ProjectNodeInfo> list = record.getNodeList();
        list.forEach(node -> {
            node.setId(UUIDBuild.getUUID());
            node.setProjectId(record.getId());
        });
        projectNodeInfoMapper.batchInsert(list);

        // 修改当前项目节点
        ScheduleTrackInfo scheduleTrackInfo = new ScheduleTrackInfo();
        scheduleTrackInfo.setId(record.getId());
        scheduleTrackInfo.setNode(list.get(0).getNodeId());
        scheduleTrackInfoMapper.updateByPrimaryKeySelective(scheduleTrackInfo);

        return JsonData.success(null, 906);
    }

    /**
     * 新增数据
     *
     * @param record
     * @return
     */
    @Override
    public JsonData insertBy(ScheduleTrackInfo record) {
        record.setId(UUIDBuild.getUUID());
        scheduleTrackInfoMapper.insert(record);
        return JsonData.success(null, 906);
    }

    /**
     * 新增数据
     *
     * @param record
     * @return
     */
    @Override
    public JsonData insertAll(ScheduleTrackInfo record) {
        scheduleTrackInfoMapper.updateTrack(record);
        return JsonData.success(null, 906);
    }

    /**
     * 修改当前项目节点
     *
     * @param record
     * @return
     */
    @Override
    public JsonData update(ScheduleTrackInfo record) {
        // 批量添加节点列表
        List<ProjectNodeInfo> list = record.getNodeList();
        list.forEach(node -> {
            node.setId(UUIDBuild.getUUID());
            node.setProjectId(record.getId());
        });
        projectNodeInfoMapper.batchInsert(list);

        // 修改当前项目节点
        ScheduleTrackInfo scheduleTrackInfo = new ScheduleTrackInfo();
        scheduleTrackInfo.setId(record.getId());
        scheduleTrackInfo.setNode(list.get(0).getNodeId());
        scheduleTrackInfoMapper.updateByPrimaryKeySelective(scheduleTrackInfo);

        return JsonData.success(null, 906);
    }


    /**
     * 根据主键查询
     *
     * @param record
     * @return
     */
    @Override
    public JsonData selectByPrimaryKey(ScheduleTrackInfo record) {

        return null;
    }


    /**
     * 更新
     *
     * @param record
     * @return
     */
    @Override
    public JsonData updateByPrimaryKeySelective(ScheduleTrackInfo record) {
        return null;
    }


    /**
     * 分页查询
     *
     * @param record
     * @return
     */
    @Override
    public PageResult selectPageList(ScheduleTrackInfo record) {
        PageHelper.startPage(record.getPageNum(), record.getPageSize());
        List<ScheduleTrackInfo> list = scheduleTrackInfoMapper.selectPageList(record);
        PageInfo<ScheduleTrackInfo> pageInfo = new PageInfo<>(list);
        Long count = pageInfo.getTotal();
        return PageResult.success(list, null, "", count);
    }

    /**
     * 批量删除
     *
     * @param list
     * @return
     */
    @Override
    public JsonData batchDelete(List<ScheduleTrackInfo> list) {
        String delFlag = "1";
        list.forEach(sch -> sch.setDelFlag(delFlag));
        scheduleTrackInfoMapper.batchDelete(list);

        // 删除关联节点信息
        list.forEach(sch -> projectNodeInfoMapper.deleteByProjectId(new ProjectNodeInfo(sch.getId())));

        return JsonData.success(null, 906);
    }

    /**
     * 查询数据
     *
     * @param record
     * @return
     */
    @Override
    public PageResult getShowScheduleNodeInfo(ScheduleTrackInfo record) {
        PageHelper.startPage(record.getPageNum(), record.getPageSize());
        List<ScheduleResult> list = scheduleTrackInfoMapper.getShowScheduleNodeInfo(record);
        PageInfo<ScheduleResult> pageInfo = new PageInfo<>(list);
        long count = pageInfo.getTotal();
        return PageResult.success(list, null, null, count);
    }
}
